
$(document).ready(function(){
	$("#hide-button-btn").hide();
	$("#hide-jump-btn").hide();
	$("#hide-control-response-btn").hide();
	
	 var options = {
	         success : function(data) {
	             $("#working-area").html(data);
	         }
	 };
	     $("#add-button-form").ajaxForm(options);
	     $("#add-page-jump-form").ajaxForm(options);
	     $("#add-control-response-form").ajaxForm(options);
});

$("#add_key_code_btn").click(function(){
	//$("#add_key_code_panel").modal('hide');
//	var keyCodes = $("input[name='keyCodes']").val();  //获取原来的值

//	var num = $("#key_codes_table tbody tr").length + 1;  //行标
//	var numTd = $("<td></td>").text(num);   //行标td

//	var button = $("#buttons option:selected").val();
//	var buttonTd = $("<td></td>").text(button);
//	keyCodes+=button;
//	keyCodes+=":";

//	var keyCode = $("#key_code").val();
//	var KeyCodeTd = $("<td></td>").text(keyCode);
//	keyCodes+=keyCode;
//	keyCodes+="|";

//	var operation = $("<button></button").addClass("btn btn-warning")
//	.attr("name","delete_key_code").text("删除").click(function(){
//	var keyCodes = $("input[name='keyCodes']").val().split("|");  //获取原来的值
//	var tds = $(this).parent().parent().children();
//	var oldValue = "";
//	oldValue += $(tds[1]).text();
//	oldValue += ":";
//	oldValue += $(tds[2]).text();  //拼成原来的字符串
//	var newKeyCodes = "";
//	$.each(keyCodes, function(index, value){
//	if (value!="" && value != oldValue){
//	newKeyCodes+=value;   
//	newKeyCodes+="|";
//	}
//	});
//	$(this).parent().parent().remove(); //删除这一行
//	$("input[name='keyCodes']").val(newKeyCodes);
//	});
//	var operationTd = $("<td></td>").append(operation);

//	var tr = $("<tr></tr>").append(numTd).append(buttonTd).append(KeyCodeTd).append(operationTd);  //拼成一行

//	$("input[name='keyCodes']").val(keyCodes); //写入隐藏的字段
//	$("#key_codes_table tbody").append(tr);  //行加到 tbody
});

$("#add_control_response_btn").click(function(){
//	$("#add_control_response_panel").modal('hide');
//	var controlResponses = $("input[name='controlResponses']").val();  //获取原来的值

//	var num = $("#control_responses_table tbody tr").length + 1;  //行标
//	var numTd = $("<td></td>").text(num);   //行标td
//	//页面
//	var page = $("#pages option:selected").val();
//	var pageTd = $("<td></td>").text(page);
//	controlResponses+=page;
//	controlResponses+=":";
//	//按键
//	var button = $("#buttons2 option:selected").val();
//	var buttonTd = $("<td></td>").text(button);
//	controlResponses+=button;
//	controlResponses+=":";
//	//控制响应函数
//	var crFunctionId = $("#control_response_functions option:selected").val();
//	var crfunctionIdInput = $("<input></input>").attr("type","hidden").attr("name","functionId").val(crFunctionId);
//	var crFunctionName = $("#control_response_functions option:selected").text();
//	var crFunctionNameTd = $("<td></td>").text(crFunctionName);
//	controlResponses+=crFunctionId;
//	controlResponses+="|";

//	var operation = $("<button></button").addClass("btn btn-warning")
//	.attr("name","delete_control_response").text("删除").click(function(){
//	var controlResponses = $("input[name='controlResponses']").val().split("|");  //获取原来的值
//	var tds = $(this).parent().parent().children();
//	var oldValue = "";
//	oldValue += $(tds[1]).text();
//	oldValue += ":";
//	oldValue += $(tds[2]).text();  //拼成原来的字符串
//	oldValue +=":";
//	oldValue += $(this).next().val();
//	var newControlResponses = "";
//	$.each(controlResponses, function(index, value){
//	if (value!="" && value != oldValue){
//	newControlResponses+=value;
//	newControlResponses+="|";
//	}
//	});
//	$(this).parent().parent().remove(); //删除这一行
//	$("input[name='controlResponses']").val(newControlResponses);
//	});

//	var operationTd = $("<td></td>").append(operation).append(crfunctionIdInput);
//	var tr = $("<tr></tr>").append(numTd).append(pageTd).append(buttonTd).append(crFunctionNameTd).append(operationTd);  //拼成一行

//	$("input[name='controlResponses']").val(controlResponses); //写入隐藏的字段
//	$("#control_responses_table tbody").append(tr);  //行加到 tbody
});

function closeDialog(dialog){
	$(dialog).modal("hide");
}

function deleteControlMapping(_mappingId, deleteBtn){
	var _projectId =  $("input[name='projectId']").val();
	var _dpcUri = $("input[name='dpcUri']").val();
	$.post('/ManEnv/controlDesignPattern/delete',
			{mappingId:_mappingId, projectId:_projectId, dpcUri:_dpcUri},
			function(data){
				if(data.result == "true"){
					alert("删除成功!");
					$(deleteBtn).parents("tr").remove(); // 删除第一个tr元素
				}else{
					alert("删除失败!");
				}
			});
}

function deletePageJump(_mappingId, deleteBtn){
	//var _mappingId = $(this).siblings('input').val();
	var _projectId =  $("input[name='projectId']").val();
	var _dpcUri = $("input[name='dpcUri']").val();
	$.post('/ManEnv/controlDesignPattern/delete',
			{mappingId:_mappingId, projectId:_projectId, dpcUri:_dpcUri},
			function(data){
				if(data.result == "true"){
					alert("删除成功!");
					 $(deleteBtn).parents("tr").remove(); // 删除第一个tr元素
				}else{
					alert("删除失败!");
				}
			});
}

function deleteButton(_buttonId, deleteBtn){
	var _projectId =  $("input[name='projectId']").val();
	var _dpcUri = $("input[name='dpcUri']").val();
	$.post('/ManEnv/controlDesignPattern/deleteButton',
			{buttonId:_buttonId, projectId:_projectId, dpcUri:_dpcUri},
			function(data){
				if(data.result == "true"){
					alert("删除成功!");
					 $(deleteBtn).parents("tr").remove(); // 删除第一个tr元素
				}else{
					alert("删除失败!");
				}
			});
}

function hideButtons(){
	$("#show-button-btn").show();
	$("#hide-button-btn").hide();
	$("#button-list-div").toggle();
}

function showButtons(){
	$("#hide-button-btn").show();
	$("#show-button-btn").hide();
	$("#button-list-div").toggle();
}

function hideJumps(){
	$("#show-jump-btn").show();
	$("#hide-jump-btn").hide();
	$("#jump-list-div").toggle();
}

function showJumps(){
	$("#hide-jump-btn").show();
	$("#show-jump-btn").hide();
	$("#jump-list-div").toggle();
}

function hideControlResponses(){
	$("#show-control-response-btn").show();
	$("#hide-control-response-btn").hide();
	$("#control-response-list-div").toggle();
}

function showControlResponses(){
	$("#show-control-response-btn").hide();
	$("#hide-control-response-btn").show();
	$("#control-response-list-div").toggle();
}


function showButtonDeletePanel(buttonId, deleteBtn){
    $("#delete-button-panel").modal({
        "show" : true,
        "backdrop" : false
    });
    $("#delete-button-btn").unbind("click");
    $("#delete-button-btn").click(function(){
    	deleteButton(buttonId, deleteBtn);
    });
}

function showControlMappingDeletePanel(crId, deleteBtn){
    $("#delete-control-mapping-panel").modal({
        "show" : true,
        "backdrop" : false
    });
    $("#delete-control-mapping-btn").unbind("click");
    $("#delete-control-mapping-btn").click(function(){
    	deleteControlMapping(crId, deleteBtn);
    });
}

function showPageJumpDeletePanel(jumpId, deleteBtn){
    $("#delete-page-jump-panel").modal({
        "show" : true,
        "backdrop" : false
    });
    $("#delete-page-jump-btn").unbind("click");
    $("#delete-page-jump-btn").click(function(){
    	deletePageJump(jumpId, deleteBtn);
    });
}